﻿using PosClient.Controls;
using PosClient.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PosClient.Service
{
    public class RowFactory
    {
        private Func<IRow> _creationDelegate;

        // 配置工厂模式
        public void SetCreationType<T>() where T : IRow, new()
        {
            TypeName = typeof(T).Name;
            _creationDelegate = () =>
            {
                var obj = new T();
                return obj;
            };
        }

        // 每次访问都创建新实例
        public IRow CreateNewRow()
        {
            if (_creationDelegate == null)
                throw new InvalidOperationException("请先调用 SetCreationType 设置创建类型");

            Console.WriteLine(_creationDelegate.Invoke().GetType());
            return _creationDelegate();
        }

        public void ChangeRow()
        {
            if (_creationDelegate.Invoke().GetType() == "PluRowPro")
            {
                SetCreationType<PluRow>();
            }
            else
            {
                SetCreationType<PluRowPro>();
            }
        }

        //用于记录控件类型
        public string TypeName { get; set; }
    }
}
